package web;

import java.io.*;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import game.*;
/**
 * Servlet implementation class Servlet
 */
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Game game;
	private Set set;
	private int dim;
	private Sequence s1;
	private Sequence s2;
    /**
     * Default constructor. 
     */
    public Servlet() {
        // TODO Auto-generated constructor stub
    	game = new Game(1, 1);
    	game.setPlayer2(2);
    	char[] charSet = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
    	set = new Set(charSet);
    	dim = 4;
    	s1 = new Sequence(dim, set);
    	s2 = new Sequence(dim, set);
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		String state = request.getParameter("state");
		
	
		if(state.equals("start")){
			
			String str = request.getParameter("sequence");
					
			boolean res = false;
			if((int)session.getAttribute("ID") == 1) {
				res = s1.setAllDigit(str);
				game.setSequence1(s1);
			}
			else {
				res = s2.setAllDigit(str);
				game.setSequence2(s2);
			}
			
			
			if( ((int)session.getAttribute("ID") == 1 && game.isSequence2()) || ((int)session.getAttribute("ID") == 2 && game.isSequence1()) ){
				
				out.print("ok");
								
				out.flush();
				out.close();
			}
			else{
				out.print("nook");
				
				out.flush();
				out.close();
			}
			
		}
			
		else if(state.equals("refresh")){
			if( ((int)session.getAttribute("ID") == 1 && game.isSequence2()) || ((int)session.getAttribute("ID") == 2 && game.isSequence1()) ){
				
				out.print("ok");
								
				out.flush();
				out.close();
			}
			else{
				out.print("nook");
				
				out.flush();
				out.close();
			}
		}
			
		else if(state.equals("play")){
			char[] try1 = new char[dim];
			
			
			String p = request.getParameter("player");
			
			
			for(int i = 0; i < p.length(); i++ ){
				try1[i] =  p.charAt(i);
			}
			
						
			game.play(try1, (int)session.getAttribute("ID"));
			
			int[] result;
			if((int)session.getAttribute("ID") == 1) result = game.getLastResult1();
			else result = game.getLastResult2();
			
			String strResult = Arrays.toString(result);
				
			out.print(strResult);

			out.flush();
			out.close();
				
		}
			
		else{
			out.println(state);
		}
		
				
	}

}
